package cn.timelost.hr.service.impl;

import cn.timelost.hr.dao.AttendanceoutDao;
import cn.timelost.hr.pojo.Attendanceout;
import cn.timelost.hr.pojo.Attendanceout2;
import cn.timelost.hr.service.AttendanceoutService;
import cn.timelost.hr.vo.UnAttendanceoutVo;
import cn.timelost.hr.vo.input.AttendanceoutForm;

import cn.timelost.hr.config.utils.SysTime;
import cn.timelost.hr.enums.ResultEnum;
import cn.timelost.hr.exception.BaseException;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "attendanceout")
public class AttendanceoutServiceImpl implements AttendanceoutService {

    @Resource
    private AttendanceoutDao attendanceoutDao;



    @Override
    public PageInfo<UnAttendanceoutVo> findUnAttendanceoutAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
//        java.sql.Date date2=new java.sql.Date(searchDate.getTime());
        List<UnAttendanceoutVo> unAttendanceoutVos = attendanceoutDao.getUnAttendanceout();
        return new PageInfo<>(unAttendanceoutVos);
    }

    @Override
    @Cacheable(key = "#personalId+'-'+#beginDate+'-'+#endDate+'-'+#pageNum+'-'+#pageSize")
    public PageInfo<Attendanceout> findAll(int pageNum, int pageSize, int personalId, Date beginDate, Date endDate) {
        if (ObjectUtils.isEmpty(beginDate)) {
            beginDate = null;
        }
        PageHelper.startPage(pageNum, pageSize);
//        java.sql.Date date2=new java.sql.Date(searchDate.getTime());
        List<Attendanceout> attendanceouts = attendanceoutDao.selectAll(personalId,beginDate, endDate);
        return new PageInfo<>(attendanceouts);
    }

    @Override
    @Cacheable(key = "#root.methodName")
    public List<Attendanceout> all() {
        return attendanceoutDao.selectAll(0, null,null);
    }

    @Override
    @Cacheable(key = "#personalId+'-'+#beginDate+'-'+#endDate+'-'+#pageNum+'-'+#pageSize")
    public PageInfo<Attendanceout2> findAllView(int pageNum, int pageSize, int personalId, Date beginDate, Date endDate) {
        if (ObjectUtils.isEmpty(beginDate)) {
            beginDate = null;
        }
        PageHelper.startPage(pageNum, pageSize);
//        java.sql.Date date2=new java.sql.Date(searchDate.getTime());
        List<Attendanceout2> attendanceouts = attendanceoutDao.selectView(personalId,beginDate, endDate);
        return new PageInfo<>(attendanceouts);
    }

    @Override
    @Cacheable(key = "#root.methodName")
    public List<Attendanceout2> allView() {
        return attendanceoutDao.selectView(0, null,null);
    }

    @Override
    public Attendanceout find(int id) {
        Attendanceout attendanceout = attendanceoutDao.selectByPrimaryKey((long) id);
        if (ObjectUtils.isEmpty(attendanceout)) {
            throw new BaseException(ResultEnum.PERSONAL_ATTENDANCE_NOT_EXIST);
        }
        return attendanceout;
    }

    @Override
    public Attendanceout signUp(AttendanceoutForm attendanceoutForm) {
        int personalId=attendanceoutForm.getPersonalId();
        Attendanceout attendanceout=new Attendanceout();
        attendanceout.setPersonId(personalId);
        SysTime sysTime=new SysTime();
        attendanceout.setDate(sysTime.getDate());
        attendanceout.setTime(sysTime.getTime());
        attendanceout.setStatus(sysTime.judge());
        return attendanceout;
    }

    /**
     * 签到
     * @param
     * @return
     */
    @Override
    public Attendanceout signUp(int personalId){
        Attendanceout attendanceout=new Attendanceout();

        SysTime sysTime=new SysTime();
        attendanceout.setId(0);
        attendanceout.setPersonId(personalId);
        attendanceout.setDate(sysTime.getDate());
        attendanceout.setTime(sysTime.getTime());
        attendanceout.setStatus(sysTime.judge());
        System.out.println("myhe"+attendanceout);
        attendanceoutDao.insert(attendanceout);
        return attendanceout;
    }

    @Override
    public void signUpUn(Integer personalId) {
        Attendanceout attendanceout=new Attendanceout();

        SysTime sysTime=new SysTime();
        attendanceout.setId(0);
        attendanceout.setPersonId(personalId);
        attendanceout.setDate(sysTime.getDate());
        attendanceout.setTime(sysTime.getTime());
        attendanceout.setStatus(0);
        System.out.println("myhe"+attendanceout);
        attendanceoutDao.insert(attendanceout);
    }
}
